﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Globalization;
using MockPrjTeam4.Entity;
using MockPrjTeam4.Biz;
using MockPrjTeam4.Common;
 
 

namespace MockPrjTeam4
{
    public partial class ContactList : System.Web.UI.UserControl
    {
        private static PagedDataSource page = new PagedDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Get list contact
                //GetListContact();
                //Check requst page
                var filename = Path.GetFileName(Request.Path);
                if (filename == "ListContact.aspx")
                {
                    //btnSelectOther.Enabled = false;
                    //btnNone.Enabled = false;
                }
                else
                {
                    btnCreate.Enabled = false;
                    //btnClose.Enabled = false;
                }
            }
            
            if (chkInactive.Checked)
            {
                gvListContact.DataSource = ContactBiz.GetListContact();
                gvListContact.DataBind();
            }
            else
            {
                gvListContact.DataSource = ContactBiz.GetListActiveContacts();
                gvListContact.DataBind();
            }
        }

      public void GetListSearch()
        {
            IList<Contacts> contacts;
            if (chkInactive.Checked)
                contacts = ContactBiz.Search(txtFirstName.Text, txtSurname.Text);
            else
                contacts = ContactBiz.Search(txtFirstName.Text, txtSurname.Text, "true");
            if (contacts != null)
            {
                gvListContact.DataSource = contacts;
                gvListContact.DataBind();
            }
        }


        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int ID = Methods.ConvertNumber(gvListContact.SelectedDataKey.Values["ContactId"].ToString());
            string firstName = ContactBiz.GetContactById(ID).FirstName;
            ContactBiz.DeleteContact(ID);
            if (chkInactive.Checked)
            {
                gvListContact.DataSource = ContactBiz.GetListContact();
                gvListContact.DataBind();
            }
            else
            {
                gvListContact.DataSource = ContactBiz.GetListActiveContacts();
                gvListContact.DataBind();
            }
            lblMessage.Text = "You deleted " + firstName;
            lblMessageResult.Text = "";
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Forms/AddContact.aspx");
            lblMessageResult.Text = "";
        }

        protected void chkInactive_CheckedChanged(object sender, EventArgs e)
        {
            if (hdfFilter.Value != "")
            {
                IList<Contacts> contact = new List<Contacts>();
                bool inActive;
                if (chkInactive.Checked) inActive = true;
                else inActive = false;
                switch (hdfFilter.Value)
                {
                    case "SearchAll":
                        {
                            if (inActive == true) contact = ContactBiz.GetListContact();
                            else contact = ContactBiz.GetListActiveContacts();
                            break;
                        }
                    case "Search09":
                        {
                            contact = ContactBiz.SearchFilter("[0-9]%", inActive);
                            break;
                        }
                    case "SearchAE":
                        {
                            contact = ContactBiz.SearchFilter("[A-E]%", inActive);
                            break;
                        }
                    case "SearchFJ":
                        {
                            contact = ContactBiz.SearchFilter("[F-J]%", inActive);
                            break;
                        }
                    case "SearchKN":
                        {
                            contact = ContactBiz.SearchFilter("[K-N]%", inActive);
                            break;
                        }
                    case "SearchOR":
                        {
                            contact = ContactBiz.SearchFilter("[O-R]%", inActive);
                            break;
                        }
                    case "SearchSV":
                        {
                            contact = ContactBiz.SearchFilter("[S-V]%", inActive);
                            break;
                        }
                    case "SearchWZ":
                        {
                            contact = ContactBiz.SearchFilter("[W-Z]%", inActive);
                            break;
                        }
                }
                
                if (contact.Count == 0)
                {
                    lblMessageResult.Text = "No more results";
                }
                else
                {
                    lblMessageResult.Text = "";
                }
                gvListContact.DataSource = contact;
                gvListContact.DataBind();
            }
            else
            {
                IList<Contacts> contactSearch;
                if (chkInactive.Checked)
                {
                    contactSearch = ContactBiz.Search(txtFirstName.Text, txtSurname.Text);
                }
                else
                {
                    contactSearch = ContactBiz.Search(txtFirstName.Text, txtSurname.Text, "true");
                }
                if (contactSearch.Count == 0)
                {
                    lblMessageResult.Text = "No more results";
                }
                else
                {
                    lblMessageResult.Text = "";
                }
                gvListContact.DataSource = contactSearch;
                gvListContact.DataBind();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IList<Contacts> contact;
            if (chkInactive.Checked)
            {
                contact = ContactBiz.Search(txtFirstName.Text, txtSurname.Text);
            }
            else
            {
                contact = ContactBiz.Search(txtFirstName.Text, txtSurname.Text, "true");
            }
            if (contact.Count == 0)
            {
                lblMessageResult.Text = "No more results";
            }
            else
            {
                lblMessageResult.Text = "";
            }
            gvListContact.DataSource = contact;
            gvListContact.DataBind();
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtFirstName.Text = "";
            txtSurname.Text = "";
            lblMessageResult.Text = "";
        }

        protected void gvListContact_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListContact.PageIndex = e.NewPageIndex;
            gvListContact.DataBind();
        }

        protected void Click(object sender, CommandEventArgs e)
        {
            hdfFilter.Value = e.CommandName;
            IList<Contacts> contact = new List<Contacts>();
            bool inActive;
            if (chkInactive.Checked) inActive = true;
            else inActive = false;
            switch (e.CommandName)
            {
                case "SearchAll":
                    {
                        if (inActive == true) contact = ContactBiz.GetListContact();
                        else contact = ContactBiz.GetListActiveContacts();
                        break;
                    }
                case "Search09":
                    {
                        contact = ContactBiz.SearchFilter("[0-9]%", inActive);
                        break;
                    }
                case "SearchAE":
                    {
                        contact = ContactBiz.SearchFilter("[A-E]%", inActive);
                        break;
                    }
                case "SearchFJ":
                    {
                        contact = ContactBiz.SearchFilter("[F-J]%", inActive);
                        break;
                    }
                case "SearchKN":
                    {
                        contact = ContactBiz.SearchFilter("[K-N]%", inActive);
                        break;
                    }
                case "SearchOR":
                    {
                        contact = ContactBiz.SearchFilter("[O-R]%", inActive);
                        break;
                    }
                case "SearchSV":
                    {
                        contact = ContactBiz.SearchFilter("[S-V]%", inActive);
                        break;
                    }
                case "SearchWZ":
                    {
                        contact = ContactBiz.SearchFilter("[W-Z]%", inActive);
                        break;
                    }
            }
            if (contact.Count == 0)
            {
                lblMessageResult.Text = "No more results";
            }
            else
            {
                lblMessageResult.Text = "";
            }
            gvListContact.DataSource = contact;
            gvListContact.DataBind();
        }
    }
}